package ms.maplestory.boot.packets.universal;

import ms.maplestory.boot.connection.OutPacket;

import java.util.Random;

public class CalcDamage {

    private transient long rndActionMan1, rndActionMan2, rndActionMan3;

    public CalcDamage() {
        setActionRndSeed(new Random().nextLong(), 1170746341L * 5 - 755606699L, 1170746341L * 5 - 755606699L);
    }

    public long setSeed() {
        long rndGenForCharacter = this.rndActionMan1;
        long rndForCheckDamageMiss = this.rndActionMan2;
        long rndForMortalBlow = this.rndActionMan3;
        long rndForSummoned = ((rndGenForCharacter & 0xFFFFFFFE) << 12) ^ ((rndGenForCharacter & 0x7FFC0 ^ (rndGenForCharacter >> 13)) >> 6);
        long rndForMob = 16 * (rndForCheckDamageMiss & 0xFFFFFFF8) ^ (((rndForCheckDamageMiss >> 2) ^ rndForCheckDamageMiss & 0x3F800000) >> 23);
        long rndGenForMob = ((rndForMortalBlow & 0xFFFFFFF0) << 17) ^ (((rndForMortalBlow >> 3) ^ rndForMortalBlow & 0x1FFFFF00) >> 8);
        return (rndForSummoned ^ rndForMob ^ rndGenForMob) & 0xFFFFFFFFL;
    }

    public void setActionRndSeed(long s1, long s2, long s3) {
        rndActionMan1 = s1 | 0x100000;
        rndActionMan2 = s2 | 0x1000;
        rndActionMan3 = s3 | 0x10;
    }

    public void encode(OutPacket out) {
        long s1 = setSeed();
        long s2 = setSeed();
        long s3 = setSeed();
        setActionRndSeed(s1, s2, s3);
        out.encodeInt((int) s1);
        out.encodeInt((int) s2);
        out.encodeInt((int) s3);
    }
}
